package com.itmuch.cloud.study.aop;

import com.alibaba.fastjson.JSONObject;
import com.itmuch.cloud.study.service.UserOptionService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by 齐丹枫 on 2017/3/8.
 */
@Aspect
@Component
public class PrintLogger {

    @Autowired
    private UserOptionService userOptionService;


    @Before("execution(* com.itmuch.cloud.study.controller.TransportController.saveDeliverySkubarcode(..)) or execution(* com.itmuch.cloud.study.controller.TransportController.deleteDeliverySkubarcode(..)) or execution(* com.itmuch.cloud.study.controller.TransportController.getPrintOrder(..))")
    public  void doBefor(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        JSONObject jsonObject  =JSONObject.parseObject((String) args[0]);
        String delivery = (String) jsonObject.get("delivery");
        String user = (String) jsonObject.get("user");
        String method = joinPoint.getSignature().getName();
        String optionType = null;
        if(method.equals("saveDeliverySkubarcode")){
            //扫描
            optionType = "SCAN";
        }else if(method.equals("deleteDeliverySkubarcode")){
            //删除重扫
            optionType = "DELTOSCAN";
        }else if(method.equals("getPrintOrder")){
            //结单
            optionType = "ENDORDER";
        }
        userOptionService.saveUserOption(user,method,delivery,optionType);
    }
}
